﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;

using System.Windows.Input;
using System.Windows.Interop;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using QQ群成员获取.Common;
using QQ群成员获取.ViewModel;

namespace QQ群成员获取
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        FormsWebBrowser webBrowser = null;
        public MainWindow()
        {
            InitializeComponent();
            
            webBrowser = new FormsWebBrowser(this.content);
            this.DataContext = new MainViewModel(webBrowser,this);
        }

        private void Grid_MouseDown(object sender, MouseButtonEventArgs e)
        {
            if (e.LeftButton == MouseButtonState.Pressed)
            {
                DragMove();
            }
            if (e.ClickCount == 2)
            {
                WinButton_Click(this.WindowState == WindowState.Maximized?this.restoreButton
                    : maxButton, null);
            }
        }

        private void WinButton_Click(object sender, RoutedEventArgs e)
        {
            Button btn = sender as Button;
            if (btn == null)
            {
                return;
            }
            switch (btn.Name)
            {
                case "minButton":
                    this.WindowState = WindowState.Minimized;
                    break;
                case "closeButton":
                    this.Close();
                    break;
                case "maxButton":
                    //this.WindowState = WindowState.Maximized;
                    this.maxButton.Visibility = Visibility.Collapsed;
                    this.restoreButton.Visibility = Visibility.Visible;
                    //this.mainGrid.Margin = new Thickness(0, 0, 0, 0);
                    max();
                    break;
                case "restoreButton":
                    //this.WindowState = WindowState.Normal;
                    //this.mainGrid.Margin = new Thickness(10, 10, 10, 10);
                    this.maxButton.Visibility = Visibility.Visible;
                    this.restoreButton.Visibility = Visibility.Collapsed;
                    max();
                    break;
                default:
                    break;
            }
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            webBrowser.WebBrowser.DocumentCompleted -= WebBrowser_DocumentCompleted;
            webBrowser.WebBrowser.DocumentCompleted += WebBrowser_DocumentCompleted;
            webBrowser.WebBrowser.Navigate(new Uri("https://qun.qq.com/member.html"));
        }

        private async void FindButton_Click(object sender, RoutedEventArgs e)
        {
            if (infoGrid.Visibility == Visibility.Visible)
            {
                return;
            }
            webBrowser.WebBrowser.DocumentCompleted -= WebBrowser_DocumentCompleted;
            webBrowser.WebBrowser.DocumentCompleted += WebBrowser_DocumentCompleted;
            //先跳转页面
            webBrowser.WebBrowser.Navigate(new Uri("https://qun.qq.com/member.html"));
            //跳转后获取
        }
        private void ShowTipInfo(string strMsg)
        {
            SolidColorBrush solidClr = Application.Current.Resources.MergedDictionaries[0]["mainColor"] as SolidColorBrush;
            Color color = (Color)ColorConverter.ConvertFromString("#212122");
            if (solidClr != null)
            {
                color = solidClr.Color;
            }
            MessageBoxWindow wind = new MessageBoxWindow(color, strMsg);
            wind.ShowDialog();
        }
        private void WebBrowser_DocumentCompleted(object sender, System.Windows.Forms.WebBrowserDocumentCompletedEventArgs e)
        {
            //这里会跳转两次 一次是原始请求 一次是原始请求触发的第一列表请求 所以我们需要解析的是 触发的请求 判断url
            var browser = sender as System.Windows.Forms.WebBrowser;
            if (browser == null)
            {
                return;
            }
            string strURL = browser.Url.ToString();
            if (string.Compare(strURL, "https://qun.qq.com/member.html", true) == 0)
            {
                return;
            }
            if (!strURL.ToLower().StartsWith("https://qun.qq.com/member.html"))
            {
                return;
            }
            string strMsg = "";
            //获取解析后的HTML  不能用documenttext 或innerhtml 否则获取到的是网页的源码
            bool b = (this.DataContext as MainViewModel).LoadGroupInfo(browser.Document.Body.OuterHtml);
            if (b == false)
            {
                strMsg = "加载群组信息失败(未登录)或该账号未加入群";
            }
            if (!string.IsNullOrWhiteSpace(strMsg))
            {
                ShowTipInfo(strMsg);
                return;
            }
            webBrowser.Hide();
            infoGrid.Visibility = Visibility.Visible;
        }

        private void datagrid1_ScrollChanged(object sender, ScrollChangedEventArgs e)
        {
            (this.DataContext as MainViewModel).ScrollViewerMember_ScrollChanged(sender, e);
        }

        private void LoginButton_Click(object sender, RoutedEventArgs e)
        {
            infoGrid.Visibility = Visibility.Collapsed;
            (webBrowser.WebBrowser.Document as System.Windows.Forms.HtmlDocument).ExecCommand("ClearAuthenticationCache", false, null);
            (this.DataContext as MainViewModel).Reset();
            webBrowser.Show();
            webBrowser.WebBrowser.Navigate(new Uri("https://qun.qq.com/member.html"));
        }

        double normaltop;
        double normalleft;
        double normalwidth;
        double normalheight;
        /// <summary>
        /// 最大化/还原处理
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void max()
        {
            //wpf最大化 全屏显示任务栏处理
            if (this.WindowState == WindowState.Normal)
            {
                normaltop = this.Top;
                normalleft = this.Left;
                normalwidth = this.Width;
                normalheight = this.Height;

                double top = SystemParameters.WorkArea.Top;
                double left = SystemParameters.WorkArea.Left;
                double right = SystemParameters.PrimaryScreenWidth - SystemParameters.WorkArea.Right;
                double bottom = SystemParameters.PrimaryScreenHeight - SystemParameters.WorkArea.Bottom;
                mainGrid.Margin = new Thickness(left, top, right, bottom);

                this.WindowState = WindowState.Maximized;
            }
            else
            {
                this.WindowState = WindowState.Normal;

                //必须先设置为0,在重新设值,若前后值一样,会失效 --拖动任务栏后,还原-始终显示在屏幕最左上方
                this.Top = 0;
                this.Left = 0;
                this.Width = 0;
                this.Height = 0;

                this.Top = normaltop;
                this.Left = normalleft;
                this.Width = normalwidth;
                this.Height = normalheight;

                mainGrid.Margin = new Thickness(10);
            }
        }
    }
}
